from sqlalchemy.orm import Session
from . import models, schemas

def get_message_notice_log(db: Session, message_notice_log_id: int):
    return db.query(models.MessageNoticeLog).filter(models.MessageNoticeLog.id == message_notice_log_id).first()

def get_message_notice_logs(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.MessageNoticeLog).offset(skip).limit(limit).all()

def create_message_notice_log(db: Session, message_notice_log: schemas.MessageNoticeLogCreate):
    db_message_notice_log = models.MessageNoticeLog(**message_notice_log.dict())
    db.add(db_message_notice_log)
    db.commit()
    db.refresh(db_message_notice_log)
    return db_message_notice_log

def update_message_notice_log(db: Session, message_notice_log_id: int, message_notice_log: schemas.MessageNoticeLogUpdate):
    db_message_notice_log = db.query(models.MessageNoticeLog).filter(models.MessageNoticeLog.id == message_notice_log_id).first()
    if db_message_notice_log:
        for var, value in vars(message_notice_log).items():
            setattr(db_message_notice_log, var, value) if value else None
        db.add(db_message_notice_log)
        db.commit()
        db.refresh(db_message_notice_log)
    return db_message_notice_log

def delete_message_notice_log(db: Session, message_notice_log_id: int):
    db_message_notice_log = db.query(models.MessageNoticeLog).filter(models.MessageNoticeLog.id == message_notice_log_id).first()
    if db_message_notice_log:
        db.delete(db_message_notice_log)
        db.commit()
    return db_message_notice_log